package vip.xiaonuo.app.modular.auth.factory;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import vip.xiaonuo.app.core.consts.AppConstant;
import vip.xiaonuo.app.core.utils.ConstellationUtil;
import vip.xiaonuo.app.modular.appauth.entity.AppAuthUser;
import vip.xiaonuo.app.modular.appauth.service.AppAuthUserService;
import vip.xiaonuo.core.pojo.login.AppLoginUser;
import vip.xiaonuo.core.util.HttpServletUtil;
import vip.xiaonuo.core.util.IpAddressUtil;
import vip.xiaonuo.core.util.UaUtil;
import vip.xiaonuo.sys.modular.file.service.SysFileInfoService;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>Title : App登录用户工厂类</p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/9/21 9:11
 */
public class AppLoginUserFactory {
    //文件
    private static final SysFileInfoService sysFileInfoService = SpringUtil.getBean(SysFileInfoService.class);
    //app用户认证相关
    private static final AppAuthUserService appAuthUserService = SpringUtil.getBean(AppAuthUserService.class);

    //组装
    public static void fillAppLoginUserInfo(AppLoginUser appLoginUser) {

        HttpServletRequest request = HttpServletUtil.getRequest();

        if (ObjectUtil.isNotNull(request)) {

            appLoginUser.setLastLoginIp(IpAddressUtil.getIp(request));
            appLoginUser.setLastLoginTime(DateTime.now().toString());
            appLoginUser.setLastLoginAddress(IpAddressUtil.getAddress(request));
            appLoginUser.setLastLoginBrowser(UaUtil.getBrowser(request));
            appLoginUser.setLastLoginOs(UaUtil.getOs(request));
            //头像url
            String headUrl = sysFileInfoService.getSysFileInfo(appLoginUser.getHeadPicId());
            appLoginUser.setHeadPicUrl(headUrl);
            //形象url
            if (ObjectUtil.isNotEmpty(appLoginUser.getIntroPicIds())) {
                List<String> introUls = sysFileInfoService.getListSysFileInfo(Arrays.asList(appLoginUser.getIntroPicIds().split(",")));
                appLoginUser.setIntroPicIdUrl(introUls);
            }
            //年龄 星座
            if (ObjectUtil.isNotEmpty(appLoginUser.getBirthday())) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String format = simpleDateFormat.format(appLoginUser.getBirthday());
                DateTime dateTime = DateUtil.parseDate(format);
                appLoginUser.setAge(DateUtil.ageOfNow(dateTime));
                appLoginUser.setConstellation(ConstellationUtil.getConstellation(format));
            }
            //是否vip
            if (appLoginUser.getVip().equals(AppConstant.VIP_ZERO)) {
                //判断日期是否到期
                int vipNum = appLoginUser.getVipTime().compareTo(new Date());
                if (vipNum > 0) {
                    appLoginUser.setVip(AppConstant.VIP_ZERO);
                    appLoginUser.setVipTime(appLoginUser.getVipTime());
                } else {
                    appLoginUser.setVip(AppConstant.VIP_ONE);
                    appLoginUser.setVipTime(null);
                }
            }
            //实名认证
            AppAuthUser REAL_AUTH = appAuthUserService.getByUseIdAndType(appLoginUser.getId(), AppConstant.REAL_AUTH);

            //真人认证


            //语音签名



//            //实名认证
//            AppAuthUser REAL_AUTH = appAuthUserService.getByUseIdAndType(appLoginUser.getId(), AppConstant.REAL_AUTH);
//            if (ObjectUtil.isNotEmpty(REAL_AUTH)) {
//                appLoginUser.setRealAuth(true);
//            }
//            //真人认证
//            AppAuthUser REAL_NAME_AUTH = appAuthUserService.getByUseIdAndType(appLoginUser.getId(), AppConstant.REAL_NAME_AUTH);
//            if (ObjectUtil.isNotEmpty(REAL_NAME_AUTH)) {
//                appLoginUser.setRealNameAuth(true);
//            }
//            //语音签名
//            AppAuthUser VOICE_SING = appAuthUserService.getByUseIdAndType(appLoginUser.getId(), AppConstant.VOICE_SING);
//            if (ObjectUtil.isNotEmpty(VOICE_SING)) {
//                //时间
//                appLoginUser.setVoiceSingTime(VOICE_SING.getVoiceSingTime());
//                //文件
//                String voiceUrl = sysFileInfoService.getSysFileInfo(Long.valueOf(VOICE_SING.getFileIds()));
//                appLoginUser.setVoiceSingUrl(voiceUrl);
//                //是否认证
//                appLoginUser.setVoiceSing(true);
//            }
        }

    }
}

